﻿using System;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Common;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.TeamFoundation.WorkItemTracking;
using Microsoft.VisualStudio.Shell;

using ScrumPowerTools.Controllers;
using ScrumPowerTools.Packaging;


namespace ScrumPowerTools
{
    /// <summary>
    /// This is the class that implements the package exposed by this assembly.
    ///
    /// The minimum requirement for a class to be considered a valid package for Visual Studio
    /// is to implement the IVsPackage interface and register itself with the shell.
    /// This package uses the helper classes defined inside the Managed Package Framework (MPF)
    /// to do it: it derives from the Package class that provides the implementation of the 
    /// IVsPackage interface and uses the registration attributes defined in the framework to 
    /// register itself and its components with the shell.
    /// </summary>
    // This attribute tells the PkgDef creation utility (CreatePkgDef.exe) that this class is
    // a package.
    [PackageRegistration(UseManagedResourcesOnly = true)]
    // This attribute is used to register the informations needed to show the this package
    // in the Help/About dialog of Visual Studio.
    [InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)]
    // This attribute is needed to let the shell know that this package exposes some menus.
    [ProvideMenuResource("Menus.ctmenu", 1)]
    [Guid(Identifiers.PackageId)]
    [ProvideAutoLoad("{e13eedef-b531-4afe-9725-28a69fa4f896}")]
    public sealed class ScrumPowerToolsPackage : Package, IOleCommandTarget
    {
        private MenuCommandController menuCommandController;
        private const int OLECMDERR_E_UNKNOWNGROUP = unchecked((int)0x80040104);

        /// <summary>
        /// Initialization of the package; this method is called right after the package is sited, so this is the place
        /// where you can put all the initilaization code that rely on services provided by VisualStudio.
        /// </summary>
        protected override void Initialize()
        {
            base.Initialize();

            var dte = (EnvDTE.DTE)GetService(typeof(EnvDTE.DTE));
            var teamExplorer = (IVsTeamExplorer)GetService(typeof(IVsTeamExplorer));
            var documentService = (DocumentService)dte.GetObject("Microsoft.VisualStudio.TeamFoundation.WorkItemTracking.DocumentService");

            new QueryResultsTotalizerController(documentService, dte.StatusBar, teamExplorer);

            menuCommandController = new MenuCommandController(dte, documentService, teamExplorer);
        }

        int IOleCommandTarget.QueryStatus(ref Guid pguidCmdGroup, uint commandId, OLECMD[] prgCmds, IntPtr pCmdText)
        {
            if (pguidCmdGroup != Identifiers.CommandGroupId)
            {
                return OLECMDERR_E_UNKNOWNGROUP;
            }

            switch (prgCmds[0].cmdID)
            {
                case MenuCommands.ShowAffectedChangesetFiles:
                case MenuCommands.ShowChangesetsWithAffectedFiles:

                    prgCmds[0].cmdf = (int)OLECMDF.OLECMDF_SUPPORTED;

                    if (menuCommandController.CanExecute(commandId))
                    {
                        prgCmds[0].cmdf |= (int)OLECMDF.OLECMDF_ENABLED;
                    }

                    break;

                default:
                    return OLECMDERR_E_UNKNOWNGROUP;
            }

            return VSConstants.S_OK;
        }

        int IOleCommandTarget.Exec(ref Guid pguidCmdGroup, uint commandId, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
        {
            if (pguidCmdGroup != Identifiers.CommandGroupId)
            {
                return OLECMDERR_E_UNKNOWNGROUP;
            }

            if (menuCommandController.Execute(commandId))
            {
                return VSConstants.S_OK;
            }

            return OLECMDERR_E_UNKNOWNGROUP;
        }
    }
}